<template>
	<uv-popup ref="popupRef" mode="bottom" round="28rpx">
		<view class="platform">
			<view class="title">接单平台</view>
			<view class="form">
				<view class="btns">
					<button class="cu-btn" :class="{ 'cu-btn-active': queryform.platform.includes(el.id) }"
						v-for="(el, n) in options" :key="el" @click="handSelect(el, n)">
						{{ el.name }}
					</button>
				</view>
				<view class="action">
					<button class="cu-btn" @click="handClose">取消</button>
					<button class="cu-btn" @click="handConfirm">确定</button>
				</view>
			</view>
		</view>
	</uv-popup>
</template>

<script setup>
	import {
		reactive,
		ref,
		defineEmits
	} from 'vue'
	import {
		doPlatform
	} from '@/apis/system.js'
	const emit = defineEmits(['change'])
	const queryform = ref({
		platform: []
	})
	const handSelect = (e, index) => {
		if (queryform.value.platform.includes(e.id)) {
			queryform.value.platform = queryform.value.platform.filter(el => el !== e.id)
		} else {
			queryform.value.platform.push(e.id)
		}
	}
	const handConfirm = () => {
		const params = uni.$uv.deepClone(queryform.value)
		const arr = options.value.filter(el => {
			return params.platform.includes(el.id)
		})
		emit('change', arr)
		handClose()
	}

	const popupRef = ref(null)
	const options = ref([])
	const handOpen = async (arr) => {
		const data = await doPlatform()
		options.value = data
		if (arr) {
			queryform.value.platform = arr.map(el => {
				return el.id
			})
		} else {
			queryform.value.platform = []
		}
		popupRef.value.open()
	}
	const handClose = () => {
		popupRef.value.close()
	}
	defineExpose({
		handOpen
	})
</script>

<style lang="scss" scoped>
	.platform {
		padding: 30rpx 24rpx;
		padding-bottom: 58rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 58rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 58rpx);

		.title {
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
		}

		.form {
			margin-top: 44rpx;

			.btns {
				display: flex;
				flex-wrap: wrap;
				gap: 20rpx;
				margin-top: 24rpx;

				.cu-btn {
					height: 68rpx;
					background: #F5F5F5;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
					margin: 0;
					border: 1rpx solid #F5F5F5;

					&::after {
						border: none;
					}

					&-active {
						background: #FFF7E4;
						color: #FF930D;
						border-color: #FF930D;
					}
				}
			}

			.action {
				margin-top: 48rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.cu-btn {
					width: 332rpx;
					height: 94rpx;
					background: #F5F5F5;
					border-radius: 16rpx;
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;

					&:last-child {
						background: #FFC434;
					}
				}
			}
		}
	}
</style>